'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _class;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _radium = require('radium');

var _radium2 = _interopRequireDefault(_radium);

var _lightbiFrontWidget = require('lightbi-front-widget');

var _lightbiFrontUtils = require('lightbi-front-utils');

var _lightbiFrontProject = require('lightbi-front-project');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var LoginForm = (0, _radium2.default)(_class = function (_React$Component) {
  _inherits(LoginForm, _React$Component);

  function LoginForm(props) {
    _classCallCheck(this, LoginForm);

    var _this = _possibleConstructorReturn(this, (LoginForm.__proto__ || Object.getPrototypeOf(LoginForm)).call(this, props));

    _this.state = { isSubmit: false, errorMsg: "" };
    _this.onSubmit = _this.onSubmit.bind(_this);
    _this.afterSubmitResolve = _this.afterSubmitResolve.bind(_this);
    _this.afterSubmitReject = _this.afterSubmitReject.bind(_this);
    _this.gotoForgetPassword = _this.gotoForgetPassword.bind(_this);
    _this.gotoRegister = _this.gotoRegister.bind(_this);
    _this.jumpUri = _this.props.location.query['jumpUri'];
    return _this;
  }

  _createClass(LoginForm, [{
    key: 'afterSubmitResolve',
    value: function afterSubmitResolve(userVoucher) {
      if (!userVoucher.getIsAdmin()) {
        this.afterSubmitReject('非管理员账户, 不能登录');
        return;
      }

      _lightbiFrontUtils.LoginInfo.login(userVoucher.getEnterpriseId(), userVoucher.getUserId(), userVoucher.getJwt(), false, true);

      if (this.jumpUri == null || this.jumpUri.length == 0) (0, _lightbiFrontUtils.Goto)('/admin/projects', this.props.router);else (0, _lightbiFrontUtils.Goto)(_lightbiFrontUtils.Base64.decode(this.jumpUri), this.props.router);
    }
  }, {
    key: 'afterSubmitReject',
    value: function afterSubmitReject(errorMsg) {
      var _this2 = this;

      this.setState({ isSubmit: false, errorMsg: errorMsg });
      setTimeout(function () {
        _this2.setState({ errorMsg: null });
      }, 1000);
    }
  }, {
    key: 'onSubmit',
    value: function onSubmit() {
      var _this3 = this;

      if (this.refs.uniqueId.isValid() && this.refs.password.isValid()) {
        this.setState({ isSubmit: true });
        var uniqueId = this.refs.uniqueId.getValue();
        var type = _lightbiFrontProject.UniqueType.EMAIL;
        if (uniqueId.indexOf('@') == -1) type = _lightbiFrontProject.UniqueType.MOBILE;

        _lightbiFrontProject.UserController.login(true, this.refs.uniqueId.getValue(), type, this.refs.password.getValue(), this.afterSubmitResolve, this.afterSubmitReject);
      } else if (!this.refs.uniqueId.isValid()) {
        this.setState({ errorMsg: '请输入注册手机' });
        setTimeout(function () {
          _this3.setState({ errorMsg: null });
        }, 1000);
      } else if (!this.refs.password.isValid()) {
        this.setState({ errorMsg: '请输入有效密码' });
        setTimeout(function () {
          _this3.setState({ errorMsg: null });
        }, 1000);
      }
    }
  }, {
    key: 'gotoForgetPassword',
    value: function gotoForgetPassword() {
      if (this.jumpUri == null || this.jumpUri.length == 0) this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/forgetPassword');else this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/forgetPassword?jumpUri=' + this.jumpUri);
    }
  }, {
    key: 'gotoRegister',
    value: function gotoRegister() {
      if (this.jumpUri == null || this.jumpUri.length == 0) this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/register');else this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/register?jumpUri=' + this.jumpUri);
    }
  }, {
    key: 'render',
    value: function render() {
      var buttonClass = "btn btn-primary " + (this.state.isSubmit ? " disabled " : "");
      var buttonDiv = null;
      if (this.state.isSubmit) buttonDiv = _react2.default.createElement(
        'button',
        { className: buttonClass, style: { marginRight: '5px', marginTop: '10px', minWidth: '120px' }, type: 'submit', onClick: this.onSubmit },
        '\u767B\u5F55\u4E2D',
        _react2.default.createElement('i', { className: 'fa fa-spinner fa-spin', style: { marginLeft: '5px' } })
      );else buttonDiv = _react2.default.createElement(
        'button',
        { className: buttonClass, style: { marginRight: '5px', marginTop: '10px', minWidth: '120px' }, type: 'submit', onClick: this.onSubmit },
        '\u767B\u5F55'
      );

      return _react2.default.createElement(
        'div',
        { style: { width: '496px', height: '100%', display: 'inline-block', position: 'relative', marginTop: '60px', padding: '70px', '@media (max-width: 767px)': { width: '100%', paddingLeft: '15px', paddingRight: '15px', paddingTop: '40px' } } },
        _react2.default.createElement(
          'div',
          { style: { paddingBottom: '15px', fontSize: '20px', fontWeight: 'bold' } },
          '\u767B\u5F55'
        ),
        _react2.default.createElement(
          'div',
          { style: { paddingTop: '15px', position: 'relative' } },
          _react2.default.createElement(
            'div',
            { style: { marginBottom: '10px', width: '100%' } },
            _react2.default.createElement(_lightbiFrontWidget.BoxInput, { ref: 'uniqueId', placeholder: '\u4F01\u4E1A\u6CE8\u518C\u624B\u673A\u53F7', label: '\u8D26\u53F7', validateFunc: _lightbiFrontUtils.mobileOrEmailValidate, isReadOnly: this.state.isSubmit })
          ),
          _react2.default.createElement(_lightbiFrontWidget.BoxInput, { ref: 'password', type: 'password', placeholder: '\u5927\u4E8E6\u4F4D', label: '\u5BC6\u7801', validateFunc: _lightbiFrontUtils.loginPasswordValidate, isReadOnly: this.state.isSubmit }),
          _react2.default.createElement(
            'div',
            { className: 'row', style: { paddingTop: '20px', paddingBottom: '20px', margin: '0px' } },
            _react2.default.createElement(
              'div',
              { className: 'col-xs-6', style: { textAlign: 'left' } },
              _react2.default.createElement(
                'a',
                { style: { cursor: 'pointer' }, onClick: this.gotoForgetPassword },
                '\u5FD8\u8BB0\u5BC6\u7801?'
              )
            ),
            _react2.default.createElement(
              'div',
              { className: 'col-xs-6 text-right' },
              _react2.default.createElement(
                'a',
                { style: { cursor: 'pointer' }, onClick: this.gotoRegister },
                '\u672A\u6CE8\u518C?'
              )
            )
          ),
          _react2.default.createElement(
            'div',
            { style: { textAlign: 'center' } },
            _react2.default.createElement(
              'div',
              { style: { position: 'relative', display: 'inline-block' } },
              buttonDiv,
              !!this.state.errorMsg ? _react2.default.createElement(
                'label',
                { style: { left: '0px', bottom: '-30px', position: 'absolute', fontSize: '12px', color: '#f55753', display: 'block', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' } },
                this.state.errorMsg
              ) : null
            )
          )
        )
      );
    }
  }]);

  return LoginForm;
}(_react2.default.Component)) || _class;

module.exports = LoginForm;